Everything about Managed Extensions For C totally explained
Managed Extensions for C++ or just Managed C++ is Microsoft's set of deviations from C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions allow C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code as well as continue to interoperate with native code. Managed C++ isn't a complete standalone, or fully fledged programming language.
These managed extensions were released with Visual C++ .NET which was one of the products released with Visual Studio .NET (2002). In late 2005, the Managed C++ extensions were significantly revised to clarify and simplify syntax and expand functionality to include managed generics. These new extensions were designated C++/CLI and included in Microsoft Visual Studio 2005. The term Managed C++ and the extensions it refers to are thus deprecated and superseded by the new extensions. The information provided in this article relates to the older extensions.
"Managed" refers to that it's run in, or managed by, the .NET virtual machine that functions as a sandbox for enhanced security in the form of more runtime checks, such as buffer overrun checks. Additionally, applications written in Managed C++ compile to CIL — Common Intermediate Language — and not directly to native CPU instructions like regular C++ applications do.
Managed C++ code can interoperate with any other language also targeted for the CLR such as C# and Visual Basic .NET as well as make use of features provided by the CLR such as garbage collection. This means Managed C++ occupies a unique position in the gallery of .NET languages. It is the only language that can communicate directly with .NET languages (such as C#, VB.NET) and native C++. The other .NET languages can only communicate with C++ code via PInvoke or COM. But since Managed C++ can communicate directly in both managed and unmanaged contexts, it's often used as a "bridge".
Additional or amended functionality provided in Managed C++
Programs coded in Managed C++ provide additional functionality of the .NET Framework and the CLR. Most notable of these is garbage collection, which relieves the programmer of manual memory management, though it's important to note that garbage collection has long been available under regular C++. The garbage collector (or GC) is handled by the CLR. Memory management is executed quite quickly, but for more performance critical applications, native, unmanaged code is most likely the preferred option.
Also, C++ has evolved much over time and most software written in the language is object oriented. Managed C++ and the use of classes and class based objects remains prevalent like in Visual C++. The only major change to this in Managed C++ is that the capabilities of multiple inheritance are not supported. This is because of a limitation of the CLR. A class managed under the CLR's garbage collector can't inherit more than one class. This is explained further in other sections.
Advantages of using Managed C++ over unmanaged code
Managed and unmanaged code can be mixed together in the same .NET assembly seamlessly. This allows the programmer to keep unmanaged code that can't be ported over to the .NET Framework without re-writing it completely. Some ramifications of using this hybrid convention are present though.
Managed C++ is the only language able to natively communicate with all other .NET languages and native unmanaged C++ code. Managed C++ is thus very convenient for interoperability between programmers who use different languages, including those in the .NET theatre and those who use unmanaged C++.
Disadvantages of using Managed C++ over unmanaged code
Managed C++ introduces a lot of new keywords and syntactic conventions that can impede on the readability of managed code, especially if C++ code is included directly and interacts directly with Managed C++ code in the same assembly.
Like in Java, there's the presence of an unnecessary overhead of features that most Managed C++ programmers and programs don't really need. This will reduce performance on most applications. The overhead of Managed C++'s specific implementation of garbage collection is the main performance inhibitor. In almost all performance-critical applications, unmanaged native code is the preferred option.
Managed C++ is superseded by C++/CLI and thus obsolete as C++/CLI has been standardized.
Managed C++ doesn't support ASP.NET web applications, which is a capability supported by all languages targeting the .NET Framework, including other third party languages.
Managed C++ includes no support for generic programming (aka templates). C++/CLI is to support this though.
Disadvantages of using Managed C++ over other Managed code (C#, Visual Basic, etc.)
Managed C++ requires a slightly longer development time than other .NET languages that could be applied to projects that still produce the same result, since the implications of pointers in C++ are still required, even in managed C++ code.
Managed C++ is a language only readily usable by programmers already familiar with C++ or another derived language. Visual Basic programmers, for example, will have trouble implementing Managed C++ in their solutions, while C# programmers, though they may have some difficulty, can adopt to Managed C++ quicker.
Main Programmatic Changes in Managed C++
The following list of changes pertain to the theatre of differences in Object Oriented Programming compared to programming with unmanaged C++.
(Global change) Existing C++ to be ported over the CLR must be appended with the following:
//hello.cpp
//new using directive
using
//another using namespace directive.
using namespace System;
int main ; //will produce an error
the preceding will produce a compile error.
[Isthis an advantage when multiple derivation leads to problems. It could be interpreted as an advantage of managed code to prohibit poor technique.]
Advantages
Managed C++ supports a greater degree of reflection than regular C++, which is generally much more convenient depending on the function of the code, or what the code is intended for.
Managed C++ can interoperate with all other .NET capable languages, including other third party languages.
Managed C++ is garbage collected. In unmanaged C++, memory management and allocation is the responsibility of the programmer.
Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:
<a href="http://managed_extensions_for_c_.totallyexplained.com">Managed Extensions for C++ Totally Explained</a>
Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.